<html>
<head><meta charset="utf-8"><title>Eager skipping for iterators · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Eager.20skipping.20for.20iterators.html">Eager skipping for iterators</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209044788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Eager%20skipping%20for%20iterators/near/209044788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Eager.20skipping.20for.20iterators.html#209044788">(Sep 04 2020 at 00:35)</a>:</h4>
<p>I know that the normal answer to "how do I skip N eagerly?" is "use <code>let _ = nth(N-1);</code>", but seeing cuviper putting a comment about it in &lt;<a href="https://users.rust-lang.org/t/hide-beginning-of-vec/48274/3?u=scottmcm">https://users.rust-lang.org/t/hide-beginning-of-vec/48274/3?u=scottmcm</a>&gt; because of its non-obviousness made me wish once again that we had a more readable and discoverable way of doing that.</p>
<p><code>Skip</code> itself is a rather weird adapter, too.  It's the only one I can think of that becomes a pass-through as soon as you've read a single item from it.  So maybe there's an API in there that could be a reasonable addition if it's also a way to get the underlying iterator out of a <code>Skip</code>, not _just_ a different way to write <code>nth</code>?</p>
<p>Here's a sketch of an idea:</p>
<div class="codehilite"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;</span><span class="n">It</span>: <span class="nc">FusedIterator</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Skip</span><span class="o">&lt;</span><span class="n">It</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">eagerly</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">It</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>Note the fused requirement: writing this made me realize that using <code>nth</code> as an eager skip can be a gotcha for unfused things (and there's no <code>must_use</code> to help).  It probably needs to be more like <code>.nth(N-1)?</code> instead to work well.</p>
<p>That would simplify the URLO post's suggestion down to </p>
<div class="codehilite"><pre><span></span><code><span class="n">v</span><span class="p">.</span><span class="n">into_iter</span><span class="p">().</span><span class="n">skip</span><span class="p">(</span><span class="mi">16</span><span class="p">).</span><span class="n">eagerly</span><span class="p">()</span><span class="w"></span>
</code></pre></div>


<p>Which seems interesting.</p>
<p>Thoughts?  Suggestions?  Alternatives?  Dissents?</p>



<a name="209045693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Eager%20skipping%20for%20iterators/near/209045693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Eager.20skipping.20for.20iterators.html#209045693">(Sep 04 2020 at 00:53)</a>:</h4>
<p>in a similar space, I see people trying to get <code>vec[0]</code> by value a lot (and don't care about the rest of the vec). currently the options are:</p>
<ul>
<li><code>vec.remove(0)</code>, which is expensive if the vec is large</li>
<li><code>vec.swap_remove(0)</code>, which is not super easy to find and still copies the last element</li>
<li><code>vec.into_iter().next()</code>, which is very difficult to find if you don't know iterators well</li>
</ul>
<p>I don't know if this needs its own method, but it would be nice to document it maybe</p>



<a name="209046656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Eager%20skipping%20for%20iterators/near/209046656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Eager.20skipping.20for.20iterators.html#209046656">(Sep 04 2020 at 01:14)</a>:</h4>
<blockquote>
<p><code>vec.swap_remove(0)</code>, which is not super easy to find and still copies the last element</p>
</blockquote>
<p>That should be possible to remove as a dead store, although IDK if LLVM is smart enough to do so.</p>



<a name="209047281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Eager%20skipping%20for%20iterators/near/209047281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Eager.20skipping.20for.20iterators.html#209047281">(Sep 04 2020 at 01:29)</a>:</h4>
<p>That (the <code>swap_remove</code> one) is probably particularly difficult for LLVM to know when there are Drop calls involved, especially if those have potential panic branches.</p>
<p>To toss in another option that's probably good: <code>vec.truncate(1); vec.pop()</code>.</p>
<p>But this is just more evidence that documenting it or having a method might be good...</p>



<a name="209047896"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Eager%20skipping%20for%20iterators/near/209047896" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Eager.20skipping.20for.20iterators.html#209047896">(Sep 04 2020 at 01:43)</a>:</h4>
<p>yeah <code>truncate()</code> seems like it should generate less code for LLVM to optimize out</p>



<a name="209047900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Eager%20skipping%20for%20iterators/near/209047900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Eager.20skipping.20for.20iterators.html#209047900">(Sep 04 2020 at 01:43)</a>:</h4>
<p>fewer calls through trait methods</p>



<a name="209051275"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Eager%20skipping%20for%20iterators/near/209051275" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Eager.20skipping.20for.20iterators.html#209051275">(Sep 04 2020 at 03:15)</a>:</h4>
<p>There's <a href="https://docs.rs/itertools/0.9.0/itertools/trait.Itertools.html#method.dropping">https://docs.rs/itertools/0.9.0/itertools/trait.Itertools.html#method.dropping</a></p>



<a name="209051281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Eager%20skipping%20for%20iterators/near/209051281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Eager.20skipping.20for.20iterators.html#209051281">(Sep 04 2020 at 03:15)</a>:</h4>
<p>But a method on <code>Skip</code> is interesting</p>



<a name="209254867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Eager%20skipping%20for%20iterators/near/209254867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Eager.20skipping.20for.20iterators.html#209254867">(Sep 07 2020 at 03:52)</a>:</h4>
<p>For anyone here from <a href="https://jyn514.github.io/2020/09/05/Rust-in-2021.html">jyn514's blog post</a>, see also <a href="#narrow/stream/219381-t-libs/topic/APIs.20for.20getting.20stuff.20from.20a.20Vec.20by.20owned/near/209048053">https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/APIs.20for.20getting.20stuff.20from.20a.20Vec.20by.20owned/near/209048053</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>